/*search.js*/
var api=require("../../../config/api.js");
var util = require('../../../utils/util.js');
const app=getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    display:'none',
    keyword: '', 
    fuzzyContents:{},
    hot_display_list: [
      {
        "id": 1,
        "name": "烧烤一号"
      },
      {
        "id": 2,
        "name": "小吃店"
      },
      {
        "id": 3,
        "name": "冰激凌"
      },
      {
        "id": 4,
        "name": "太阳镜"
      },
      {
        "id": 5,
        "name": "iphoneX"
      },
      {
        "id": 6,
        "name": "烤肉饭"
      }, {
        "id":7,
        "name": "焖面"
      }
    ],
  },
  //获取输入框的值
  bindinputchange:function(e){
    let that = this
    this.setData({
      keyword: e.detail.value,
      display:'block'
    })
    //存储搜索关键字
    wx.setStorageSync('keyword', this.data.keyword)
    //获取模糊查询关键字
    util.request(api.SearchUrl, { searchOne: this.data.keyword },'POST').then(function(res)    {
       if(res.data.code == 200){
         that.setData({
           fuzzyContents: res.data.data
         })
       }
    })

  },
  //设置搜索存储
  setSearchStorage: function () {
    let data;
    let localStorageValue = [];
    let that = this
    if (this.data.keyword != '') {
      //调用API从本地缓存中获取数据  
      var searchData = wx.getStorageSync('searchData') || [];
        if(searchData.indexOf(this.data.keyword) == -1){
          searchData.push(this.data.keyword);
        }
      wx.setStorageSync('searchData', searchData)
      wx.navigateTo({
        url: '../searchResult/searchResult',
        success:function(){
           that.setData({
             display:'none'
           })
        }
      })
    }
  },
  //清空搜索记录
  clearSearchStorage: function () {
    let that = this
    wx.setStorageSync('searchData', '')
    that.onShow()
  },
  // 清空input内容
  foo:function() {
    this.setData({
      keyword:''
    })
  },   
/**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var getSearchh = wx.getStorageSync('searchData');
    this.setData({
      getSearch: getSearchh,
      keyword: ''
    })
  }

})